//
//  DiaryViewController.m
//  e家园设计
//
//  Created by lanou on 15/4/13.
//  Copyright (c) 2015年 lanou3g.com. All rights reserved.
//

#import "DiaryViewController.h"
#import "DiaryTableViewCell.h"
#import "MyConnection.h"
#import "UIScrollView+PullLoad.h"
#import "UIImageView+WebCache.h"
#import "DiarySubViewController.h"
@interface DiaryViewController ()<PullDelegate, MyConnectionDelegate>

@end

@implementation DiaryViewController

- (void)dealloc
{
    [self.tableView release];
    [self.casesArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.casesArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatTableView];
    [self creatHeaderView];
    [self pulldown];
    [self load];
    self.navigationController.navigationBar.translucent = NO;
    

    
}



- (void)creatHeaderView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 200)];
    imageView.image = [UIImage imageNamed:@"1.jpeg"];
    [self.tableView addSubview:imageView];
    [self.tableView release];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.pullDelegate = self;
    self.tableView.canPullDown = YES;
    self.tableView.canPullUp = YES;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.casesArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *a = @"1";
    DiaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:a];
    
    if (!cell) {
        cell = [[DiaryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:a];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSMutableDictionary *dic = [self.casesArray objectAtIndex:indexPath.row];
    
    NSURL *urlImage = [NSURL URLWithString:[dic objectForKey:@"image"]];
    [cell.aimage sd_setImageWithURL:urlImage];
    
    NSURL *urlUser_pic = [NSURL URLWithString:[dic objectForKey:@"user_pic"]];
    [cell.user_pic sd_setImageWithURL:urlUser_pic];
    
    cell.title.text = [dic objectForKey:@"title"];
    
    cell.styleName.text = [dic objectForKey:@"style"];
    
    cell.sizeName.text = [dic objectForKey:@"size"];
    
    
    cell.user_name.text = [dic objectForKey:@"user_name"];
   
    
    return cell;
}


- (void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        [self pulldown];
        
    } else {
        [self load];
    }
}


//?area=0&budget=0&order=0&page=1&size=0&style=0
- (void)load
{
    self.count++;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"0" forKey:@"area"];
    [dic setObject:@"0" forKey:@"budget"];
    [dic setObject:@"0" forKey:@"order"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"0" forKey:@"size"];
    [dic setObject:@"0" forKey:@"style"];
    [dic setObject:[NSString stringWithFormat:@"%ld", self.count] forKey:@"page"];
    
    [MyConnection connectWithUrl:HTTP parmaters:dic delegate:self type:1];
}

// 下拉刷新
-(void)pulldown
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"0" forKey:@"area"];
    [dic setObject:@"0" forKey:@"budget"];
    [dic setObject:@"0" forKey:@"order"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"0" forKey:@"size"];
    [dic setObject:@"0" forKey:@"style"];
    self.count = 1;
    [MyConnection connectWithUrl:HTTP parmaters:dic delegate:self type:2];
    
}

- (void)receiveDataFinished:(NSData *)data type:(NSInteger)type
{
    if (type == 1) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic2 = [dic objectForKey:@"data"];
        NSArray *arr = [dic2 objectForKey:@"cases"];
        [self.casesArray addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self.tableView stopLoadWithState:PullUpLoadState];
        
    } else if (type == 2) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic2 = [dic objectForKey:@"data"];
        NSArray *arr = [dic2 objectForKey:@"cases"];
        [self.casesArray addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self.tableView stopLoadWithState:PullDownLoadState];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiarySubViewController *subView = [[DiarySubViewController alloc] init];
    NSMutableDictionary *dic = [self.casesArray objectAtIndex:indexPath.row];
    subView.diaryId = [dic objectForKey:@"group_id"];
    [self.navigationController  pushViewController:subView animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
